﻿using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using mojoPortal.Web;
using mojoPortal.Web.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace blackhouse.mojoPortalExtend {
    public class OwnedNonCmsBasePage : NonCmsBasePage {

        #region Fields

        private PageSettings page;

        #endregion

        #region Methods

        protected bool SetPageSettingsFromCms() {
            this.page = this.TryGetCurrentPageSettings();
            if (this.page != null) {
                PageProvider provider = new PageProvider();
                provider.TrySetFromCms(this.page);
                this.AddEditControl();
                return true;
            }
            else
                return false;
        }

        private void AddEditControl() {
            AutoHidePanel ahp = Master.FindControl("ah1") as AutoHidePanel;
            if (ahp != null) {
                ahp.Controls.AddAt(1, new OwnedPageEditSettingsLink(this.page));
            }
        }

        private PageSettings TryGetCurrentPageSettings() {
            int pageId = 0;
            PageSettings page = null;
            if (Int32.TryParse(Request["pageid"], out pageId)) {
                page = new PageSettings(CacheHelper.GetCurrentSiteSettings().SiteId, pageId);
                if (page != null && page.PageId != pageId)
                    page = null;
            }
            return page;
        }

        #endregion

    }
}
